package com.share.dubbo.wssh.config;

import com.baidu.disconf.client.DisconfMgrBean;
import com.baidu.disconf.client.DisconfMgrBeanSecond;
import com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @author 孙艳强
 * 当前开启了disconf的时候,才加载这个配置文件
 * <p>
 * <p>
 * 注：所有disconf.properties配置文件中的参数，都可以通过-Dxxx=xxx的启动参数方式进行覆盖
 * 配置文件的名字必须是disconf.properties  否则无法加载
 */
@Configuration
public class DisconfConfig {
    //<!-- 使用disconf必须添加以下配置 -->
    //<bean class="com.baidu.disconf.client.DisconfMgrBean" destroy-method="destroy"
    //id="disconfMgrBean">
    //  <property name="scanPackage" value="com.sf.tdm.iov"/>
    //</bean>
    @Bean(destroyMethod = "destroy")
    public DisconfMgrBean getDisconfMgrBean() {
        DisconfMgrBean disconfMgrBean = new DisconfMgrBean();
        //你的需要被扫描的包
        disconfMgrBean.setScanPackage("com.share.dubbo");
        return disconfMgrBean;
    }

    //<bean class="com.baidu.disconf.client.DisconfMgrBeanSecond" destroy-method="destroy"
    //id="disconfMgrBean2" init-method="init">
    //</bean>
    @Bean(destroyMethod = "destroy", initMethod = "init")
    public DisconfMgrBeanSecond getDisconfMgrBean2() {
        return new DisconfMgrBeanSecond();
    }


    /*实例化配置加载工厂*/
    @Bean(name = "reloadablePropertiesFactoryBean")
    @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
    public ReloadablePropertiesFactoryBean reloadablePropertiesFactoryBean() {
        ReloadablePropertiesFactoryBean propertiesFactoryBean = new ReloadablePropertiesFactoryBean();
        propertiesFactoryBean.setSingleton(true);

        // disconf配置的文件
        List<String> fileNames = new ArrayList<>();
        fileNames.add("classpath:mybatis.cfg.xml");
        fileNames.add("classpath:dubbo.properties");
        fileNames.add("classpath:logback-spring.xml");
        propertiesFactoryBean.setLocations(fileNames);
        return propertiesFactoryBean;
    }

    /*加载配置---配置文件的名字必须是disconf.properties  否则无法加载*/
    @Bean(name = "propertyPlaceholderConfigurer")
    public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer(ReloadablePropertiesFactoryBean reloadablePropertiesFactoryBean) {
        PropertyPlaceholderConfigurer placeholderConfigurer = new PropertyPlaceholderConfigurer();
        placeholderConfigurer.setIgnoreResourceNotFound(true);
        placeholderConfigurer.setIgnoreUnresolvablePlaceholders(true);
        try {
            Properties properties = reloadablePropertiesFactoryBean.getObject();
            placeholderConfigurer.setProperties(properties);
        } catch (IOException e) {
            throw new RuntimeException("unable to find properties", e);
        }
        return placeholderConfigurer;
    }
}
